package paper.token;

import paper.token.TT;
import paper.parse.TokenInputStream;

public class Token {
    public int from, length;     // Beginning index and length of this token
    public int lengthOfWord;     // Length of the proper word part
    public TokenInputStream parent;   // Tokenstream that contains the data.
 
    public TT t;
    public double[] probs;
    public double indent;
    public double spacing;

    public Token(TokenInputStream ts, int from, int length, int lengthOfWord,
                 TT type) {
        this.parent = ts;
        this.from = from;
        this.length = length;
        this.lengthOfWord = lengthOfWord;
        this.t = type;
    }

    public Token(TokenInputStream ts, int from, int length, int lengthOfWord,
                 TT type, double indent, double spacing) {
        this.parent = ts;
        this.from = from;
        this.length = length;
        this.lengthOfWord = lengthOfWord;
        this.t = type;
        this.indent = indent;
        this.spacing = spacing;
    }

    public String getCleanText() {
        return parent.substring(from, from+lengthOfWord).toLowerCase();
    }

    public String getRawText() {
        return parent.substring(from, from+length);
    }

    public double majorityProbability() {
        return probs[t.intValue()];
    }

    private String hex2(double x) {
        String z = Integer.toString(((int)(255*x)),16);
        return z.length() == 1 ? "0" + z : z;
    }

    public String toString() {
        return "{" + t + "}" + getRawText();//            open + text + close;
    }
}
